package com.miku.project.service.impl.inner;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.miku.mikuapicommon.model.entity.InterfaceInfo;
import com.miku.mikuapicommon.service.InnerInterfaceInfoService;
import com.miku.project.common.ErrorCode;
import com.miku.project.exception.BusinessException;
import com.miku.project.service.InterfaceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService
@Slf4j
public class InnerInterfaceInfoServiceImpl implements InnerInterfaceInfoService {
   @Resource
   private InterfaceInfoService interfaceInfoService;

    /**
     * 获取接口信息
     *
     * @param path   路径
     * @param method 方法
     * @return {@link InterfaceInfo}
     */
    @Override
    public InterfaceInfo getInterfaceInfo(String path, String method) {
        //检查参数
        if (StringUtils.isAnyBlank(path, method)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }

        //如果带参数，去除第一个?和之后后的参数
        if (path.contains("?")){
            path=path.substring(0,path.indexOf("?"));
        }
        if (path.startsWith("http://")){
            path=path.substring(7);
        }
        if (path.startsWith("https://")){
            path=path.substring(8);
        }
        log.info("【查询地址】:{}",path);
        return interfaceInfoService.getOne(Wrappers.<InterfaceInfo>lambdaQuery()
                .eq(InterfaceInfo::getMethod, method)
                .like(InterfaceInfo::getUrl, path));
    }
}
